<!--
 * @Author: Aaron
 * @Date: 2022-05-25 15:33:59
 * @LastEditors: Aaron
 * @LastEditTime: 2022-06-02 13:58:19
 * @Description: file content
 * @FilePath: \ias_ui\src\views\freshOne\index.vue
 * @Company: 沃尔玛
-->
<template>
  <iframe v-if="iframeSrc" style="position: absolute; left: 0; top: 0; width: 100%; height: 100%" :src="iframeSrc" frameborder="0" />
</template>

<script>
import store from '@/store'
export default {
  name: 'Iframe',
  data() {
    return {
      iframeSrc: ''
    }
  },
  created() {
    const token = store.getters.token
    const userId = store.getters.userId
    // const userName = store.getters.userName
    const rouerPath = this.$route.path
    this.iframeSrc = `${window.freshOne}/freshone/#${rouerPath}?token=${token}&userName=${userId}&t=${+new Date()}`
    window.addEventListener('message', this.onPostMessage, false)
  },
  beforeDestroy() {
    window.removeEventListener('message', this.onPostMessage)
  },
  methods: {
    onPostMessage(e) {
      if (window.freshOne.startsWith(e.origin) && e.data === 'logout') {
        store.dispatch('FedLogOut').then(() => {
          location.reload()
        })
      }
    }
  }
}
</script>
